package jhh.service;

import java.io.File;
import jhh.entity.Jmx;
import org.dom4j.Document;
import org.dom4j.Element;
import utils.Dom4jUtils;


public class NewJmx {
    public void createJmx(Jmx jmx,String TemplePath,String path){
    	Document document=Dom4jUtils.getDocument(TemplePath);
    	Element TestPlan=(Element) document.selectNodes("//TestPlan").get(0);
    	TestPlan.attribute("testname").setValue(jmx.getTestplan());
    	Element ThreadGroup=(Element) document.selectNodes("//ThreadGroup").get(0);
    	ThreadGroup.attribute("testname").setValue(jmx.getThreadGroup());
    	Element sample_error=(Element) document.selectNodes("//stringProp[@name='ThreadGroup.on_sample_error']").get(0);
    	sample_error.setText(jmx.getSample_error());
    	//BBBr@id='bl'表示元素名称是 BBB，在BBB上面有id属性，并且id的属性值是 bl
    	//BBB[@id1]：表示只要BBB元素上面有id属性，都得到
    	Element loops=(Element) document.selectNodes("//stringProp[@name='LoopController.loops']").get(0);
    	loops.setText(String.valueOf(jmx.getLoop()));
    	Element num_threads=(Element) document.selectNodes("//stringProp[@name='ThreadGroup.num_threads']").get(0);
    	num_threads.setText(String.valueOf(jmx.getNum_threads()));
    	Element HTTPSamplerProxy=(Element) document.selectNodes("//HTTPSamplerProxy").get(0);
    	HTTPSamplerProxy.attribute("testname").setValue(jmx.getHTTPSamplerProxy());
    	Element HTTPSampler_domain=(Element) document.selectNodes("//stringProp[@name='HTTPSampler.domain']").get(0);
    	HTTPSampler_domain.setText(jmx.getHTTPSampler_domain());
    	Element HTTPSampler_port=(Element) document.selectNodes("//stringProp[@name='HTTPSampler.port']").get(0);
    	HTTPSampler_port.setText(String.valueOf(jmx.getHTTPSampler_port()));
    	Element HTTPSampler_path=(Element) document.selectNodes("//stringProp[@name='HTTPSampler.path']").get(0);
    	HTTPSampler_path.setText(jmx.getHTTPSampler_path());
    	Element HTTPSampler_method=(Element) document.selectNodes("//stringProp[@name='HTTPSampler.method']").get(0);
    	HTTPSampler_method.setText(jmx.getHTTPSampler_method());
    	String resPath=path+File.separator+jmx.getThreadGroup()+".jmx";
    	Dom4jUtils.xmlWriter(resPath, document);
    }
}
